<?php
/**
 * Category.php
 *
 * 用户相册分类数据对象模型类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Model
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Model_Abstract */
//require_once 'Abstract.php';

/**
 * 用户相册分类模型类， 该类通过用户相册分类信息的读取， 修改， 保存等接口.
 *
 * @category 	Momoger
 * @package		Momoger_Model
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Model_Category extends Momoger_Model_Abstract {

	/**
	 * 构造函数.
	 *
	 * @param mixed $options 快速设置参数.
	 */
	public function __construct($options = null) {
        parent::__construct($options);
	}

	/**
	 * 获取一个分类信息
	 *
	 * @param int $category_id 分类ID
	 * @return array
	 */
	public function getCategory($category_id = 0) {
		return $this->findRow("categoryID = $category_id")->getAll();
	}

	/**
	 * 获取所有 分类信息
	 *
	 * @param int $user_id 用户ID
	 * @param string $type 分类类型
	 * @param array $order 分类排序
	 * @param int $limit 数目限制
	 * @return array
	 */
	public function getCategories($user_id = 0, $type = null, $order = array('categoryOrder DESC', 'categoryID ASC'), $limit = 1000) {
		$where = array('userID = ' . (int)$user_id, 'categoryActived = 1');
    	if ($type && in_array($type, array('article', 'photo'))) {
    		array_push($where, "categoryType = '$type'");
    	}
    	$models = $this->findAll($where, $order, $limit);
    	$categories = array();
		foreach ($models as $model) {
			$categories[] = $model->getAll();
		}
		return $categories;
	}

	/**
	 * 获取所有有内容的分类信息.
	 *
	 * @param int $user_id 用户ID
	 * @param string $type 分类类型
	 * @param int $limit 数目限制
	 * @return array
	 */
	public function getCategoriesForPublic($user_id = 0, $type = null, $limit = 100) {
		$where = array();
		$order = array('categoryUpdated DESC', 'categoryCounts DESC');
		if ($user_id) {
			array_push($where, 'userID = ' . (int)$user_id);
			array_unshift($order, 'categoryOrder DESC');
		}
		if ($type && in_array($type, array('article', 'photo'))) {
    		array_push($where, "categoryType = '$type'");
    	}
		array_push($where, 'categoryCounts > 0');
		$models = $this->findAll($where, $order, $limit);
    	$categories = array();
		foreach ($models as $model) {
			$categories[] = $model->getAll();
		}
		return $categories;
	}

    /**
     * 检查用户分类信息是否存在, 存在返回 true， 否则返回 false
     *
     * @param int $user_id 用户ID
     * @param string $name 用户分类信息名
     * @param string $type 用户分类类型
     * @return boolean
     */
    public function categoryIsExists($user_id = 0, $name = null, $type = 'article') {
    	if (!$name) {
    		return FALSE;
    	}
    	$where = array(
    		"userID = $user_id",
    		"categoryType = '$type'",
    		"categoryName = '$name'"
    	);
    	$this->findRow($where);
    	if ((int)$this->get('categoryID')) {
    		return TRUE;
    	}
    	return FALSE;
    }

    /**
     * 获取数据地图对象.
     *
     * @return Momoger_Model_Mapper_Category
     */
    protected function getMapper() {
        if (null === $this->_mapper) {
            $this->setMapper(new Momoger_Model_Mapper_Category());
        }
        return $this->_mapper;
    }
}